<?php

namespace App\Http\Controllers\Home\User;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\Home\Recruit as RecruitModel;
use App\Models\Home\Users as UsersModel;
use Illuminate\Support\Facades\Hash;
use Recruit;
use Frame;
use Tools;
use Auth;

class RecruitController extends Controller
{
    protected $passField = [
        'chinese_name','english_name','url','contacts','duties','sex','phone','company_phone','email','company_address','area','brief','uid','status','create_username','create_user_password'
    ];

    public function recruit_list(Request $request)
    {
        $page = $request->input('page');

        $page_info = Tools::page_helper($page,config('base.home_limit.base_limit.render_limit'),config('base.home_limit.base_limit.limit'));

        $list = Recruit::getList($page_info['skip'],$page_info['limit']);

        $count = Recruit::getCount();

        $num = intval(ceil($count/config('base.home_limit.base_limit.limit')));

        return view('Home.User.Channel.Recruit.recruit_list',compact('list','page','num'));
    }

    public function add(Request $request){
        if($request->isMethod('post')){
            //验证
            $this->validate(request(), [
                'chinese_name' => 'nullable|string|unique:recruit',
                'english_name' => 'nullable|string',
                'url' => 'nullable|string',
                'contacts' => 'nullable|string',
                'duties' => 'nullable|string',
                'sex' => 'integer',
                'phone' => 'nullable|string',
                'company_phone' => 'nullable|string',
                'email' => 'nullable|string',
                'company_address' => 'nullable|string',
                'area' => 'nullable|string',
                'brief' => 'nullable|string',
                'create_username' => 'required|string',
                'create_user_password' => 'required|string'

            ],[],[
                'chinese_name'=>'招聘网站',
                'english_name'=>'英文名称',
                'url'=>'招聘渠道网址',
                'contacts'=>'联系人',
                'duties'=>'职务',
                'sex'=>'性别',
                'phone'=>'手机',
                'company_phone'=>'公司电话',
                'email'=>'Email',
                'company_address'=>'公司地址',
                'area'=>'覆盖地区',
                'brief'=>'渠道简介',
                'create_username'=>'注册账号',
                'create_user_password'=>'注册账号密码'
            ]);

            //数据库操作
            $data = [];
            foreach ($request->all() as $key => $val){
                if (!is_numeric($val)){	$val = clean($val);}
                if(in_array($key,$this->passField)){
                    $data[$key] = $val;
                }
            }

            $data['uid'] = Auth::id();
            $res = RecruitModel::create($data);

            return ['code'=>1];
            //返回结果
        }

        return view('Home.User.Channel.Recruit.recruit_add');
    }

    public function edit(RecruitModel $obj){
        if(request() ->isMethod('post')){
            //验证
            $this->validate(request(), [
                'chinese_name' => 'nullable|string|unique:recruit',
                'english_name' => 'nullable|string',
                'url' => 'nullable|string',
                'contacts' => 'nullable|string',
                'duties' => 'nullable|string',
                'sex' => 'integer',
                'phone' => 'nullable|string',
                'company_phone' => 'nullable|string',
                'email' => 'nullable|string',
                'company_address' => 'nullable|string',
                'area' => 'nullable|string',
                'brief' => 'nullable|string',
                'create_username' => 'required|string',
                'create_user_password' => 'required|string'

            ],[],[
                'chinese_name'=>'招聘网站',
                'english_name'=>'英文名称',
                'url'=>'招聘渠道网址',
                'contacts'=>'联系人',
                'duties'=>'职务',
                'sex'=>'性别 1男,2女,3不详',
                'phone'=>'手机',
                'company_phone'=>'公司电话',
                'email'=>'Email',
                'company_address'=>'公司地址',
                'area'=>'覆盖地区',
                'brief'=>'渠道简介',
                'create_username'=>'注册账号',
                'create_user_password'=>'注册账号密码'
            ]);

            //数据库操作
            foreach (request()->all() as $key => $val){
                if (!is_numeric($val)){	$val = clean($val);}
                if(in_array($key,$this->passField)){
                    $obj->$key = $val;
                }
            }

            $res = $obj->save();
            //返回结果
            return ['code'=>1];
        }

        $obj->load(['user']);

        return view('Home.User.Channel.Recruit.recruit_edit',compact('obj'));
    }

}